import time

from PyQt5.QtCore import QThread, pyqtSignal


class UIWorker(QThread):
    current_time_signal = pyqtSignal(int)
    init_signal = pyqtSignal(list)
    first_signal = pyqtSignal(list)
    second_signal = pyqtSignal(list)
    third_signal = pyqtSignal(list)

    output1_signal = pyqtSignal(str)

    def __init__(self, mission):
        super(UIWorker, self).__init__()
        self.mission = mission

    def run(self):
        print("receive run")
        current_time = 0
        self.init_signal.emit(list(self.mission.zero_level_queue.queue))
        self.first_signal.emit(list(self.mission.first_level_queue.queue))
        self.second_signal.emit(list(self.mission.second_level_queue.queue))
        self.third_signal.emit(list(self.mission.third_level_queue.queue))
        while True:
            time.sleep(1)
            self.current_time_signal.emit(current_time)

            self.mission.run()

            self.init_signal.emit(list(self.mission.zero_level_queue.queue))
            self.first_signal.emit(list(self.mission.first_level_queue.queue))
            self.second_signal.emit(list(self.mission.second_level_queue.queue))
            self.third_signal.emit(list(self.mission.third_level_queue.queue))

            queue_list = list(self.mission.finish_queue.queue)
            finished_str = ""
            for item in queue_list:
                # 访问元素的属性并输出内容
                finished_str += "    {}         {}         {}         {}    \n".format(item.name, item.receive_time, item.need_time, item.end_time)

            self.output1_signal.emit("进程名 | 到达时间 | 运行时间 | 结束时间\n" + finished_str)

            current_time += 1
            if self.mission.check() == 0:
                break
        print("finish")
